﻿using System;
using UnityEditor;
using UnityEngine;

namespace Runtime.Common.Editor
{
    public static class SerializedPropertyEx
    {
        public delegate bool ArraySerializedMatch(SerializedProperty property, int index);

        public static void ReMoveAtIndex(this SerializedProperty serializedProperty, int index)
        {
            try
            {
                serializedProperty.DeleteArrayElementAtIndex(index);
            }
            catch (Exception e)
            {
                Debug.LogError($"{serializedProperty}===={e}");
            }
           
        }


        public static void ForEach(this SerializedProperty serializedProperty,
            System.Func<SerializedProperty, bool> match,
            bool enterChild = false)
        {
            try
            {
                serializedProperty.Next(true);
                while (serializedProperty.Next(enterChild))
                {
                    if (match(serializedProperty))
                    {
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"{serializedProperty}===={e}");
            }
        }

        public static void ArrayForEach(this SerializedProperty serializedProperty,
            ArraySerializedMatch match, bool enterChild = false)
        {
            try
            {
                if (serializedProperty == null || !serializedProperty.isArray)
                {
                    return;
                }

                for (int i = 0; i < serializedProperty.arraySize; i++)
                {
                    SerializedProperty property = serializedProperty.GetArrayElementAtIndex(i);
                    if (match(property, i))
                    {
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"{serializedProperty}===={e}");
            }
           
            // serializedProperty.Next(true);
            // while (serializedProperty.Next(enterChild))
            // {
            //     match(serializedProperty);
            // }
        }

        public static SerializedProperty AddList(this SerializedProperty serializedProperty)
        {
            try
            {
                if (serializedProperty == null || !serializedProperty.isArray)
                {
                    return null;
                }

                int index = serializedProperty.arraySize;
                serializedProperty.arraySize = index + 1;
                //serializedProperty.InsertArrayElementAtIndex(index);
                return serializedProperty.GetArrayElementAtIndex(index);
            }
            catch (Exception e)
            {
                Debug.LogError($"{serializedProperty}===={e}");
                return null;
            }
        }

        public static SerializedProperty AddAtIndex(this SerializedProperty serializedProperty, int index)
        {
            if (serializedProperty == null || !serializedProperty.isArray)
            {
                return null;
            }

            int curIndex = serializedProperty.arraySize;
            curIndex = index < curIndex ? index : curIndex;
            serializedProperty.InsertArrayElementAtIndex(curIndex);
            return serializedProperty.GetArrayElementAtIndex(curIndex);
        }

        public static void GetEntryType(this SerializedProperty serializedProperty)
        {
        }
    }
}